package day010;

import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        Stack<Animal> stack = new Stack<Animal>();
        OneHand root = new OneHand();
        root.value = 1;
        stack.add(root);
        for (int i = 2; i <= 10; i++) {
            Animal pop = stack.pop();
            if (pop instanceof OneHand) {
                TwoHand child = new TwoHand();
                child.value = i;
                ((OneHand) pop).child = child;
                stack.push(child);
            } else if (pop instanceof TwoHand) {
                OneHand left = new OneHand();
                left.value = i;
                stack.push(left);
                i++;
                ((TwoHand) pop).leftChild = left;
                TwoHand right = new TwoHand();
                right.value = i;
                ((TwoHand) pop).rightChild = right;
                stack.push(right);
            }
        }
        Stack<Animal> stackPrint = new Stack<Animal>();
        stackPrint.push(root);
        System.out.println(root.value);
        while (!stackPrint.empty()) {
            Animal pop = stackPrint.pop();
            if (pop instanceof OneHand) {
                if (((OneHand) pop).child != null) {
                    System.out.println(((OneHand) pop).child.value);
                    stackPrint.push(((OneHand) pop).child);
                }
            } else if (pop instanceof TwoHand) {
                if (((TwoHand) pop).leftChild != null) {
                    stackPrint.push(((TwoHand) pop).leftChild);
                    System.out.println(((TwoHand) pop).leftChild.value);
                }
                if (((TwoHand) pop).rightChild != null) {
                    stackPrint.push(((TwoHand) pop).rightChild);
                    System.out.println(((TwoHand) pop).rightChild.value);
                }
            }
        }
    }
}

interface Animal {
}

class OneHand implements Animal {
    public int value;
    public TwoHand child;
}

class TwoHand implements Animal {
    public int value;
    public OneHand leftChild;
    public TwoHand rightChild;
}